package com.fowo.api.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.R;
import com.fowo.api.sys.entity.SysRole;
import com.fowo.api.sys.entity.SysRoleData2;
import com.fowo.api.sys.model.*;
import com.fowo.api.sys.service.SysRoleData2Service;
import com.fowo.api.sys.service.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Tag(name = "系统角色")
@RequestMapping("/api/sys/role")
@RestController
public class SysRoleController {

    @Resource
    private SysRoleService roleService;

    @Resource
    private SysRoleData2Service data2Service;

    @Operation(summary = "全部（不分页）")
    @GetMapping
    public R<List<SysRoleItemVo>> search(SysRoleSearchParamPo search) {
        return R.data(roleService.search(search));
    }

    @Operation(summary = "查询分页")
    @GetMapping("page")
    public R<Page<SysRoleItemVo>> page(SysRoleSearchParamPo search) {
        return R.data(roleService.searchPage(search));
    }

    @Operation(summary = "获取指定 id 的记录")
    @GetMapping("{id}")
    public R<SysRoleVo> getById(@PathVariable Long id) {
        return R.data(roleService.getVoById(id));
    }

    @Operation(summary = "获取指定 id 的数据权限")
    @GetMapping("{id}/data")
    public R<SysRoleDataVo> getDataById(@PathVariable Long id) {
        return R.data(roleService.getDataVoById(id));
    }

    @Operation(summary = "更新指定 id 的数据权限")
    @PutMapping("{id}/data")
    public R<?> updateDataById(@PathVariable Long id, @RequestBody SysRoleDataVo dataVo) {
        return R.run(() -> roleService.updateData(id, dataVo));
    }

    @Operation(summary = "创建")
    @PostMapping
    public R<SysRole> create(@Validated @RequestBody CreateSysRole model) {
        return R.data(() -> roleService.create(model));
    }

    @Operation(summary = "复制")
    @PostMapping("copy")
    public R<SysRole> copy(@Validated @RequestBody CopySysRole model) {
        return R.data(() -> roleService.copy(model));
    }

    @Operation(summary = "更新")
    @PutMapping
    public R update(@RequestBody SysRoleVo sysRoleVo) {
        return R.ok(() -> roleService.update(sysRoleVo));
    }

    @Operation(summary = "删除")
    @DeleteMapping("{id}")
    public R deleteById(@PathVariable Long id) {
        return R.run(() -> roleService.removeById(id));
    }

    @Operation(summary = "获取角色的数据权限配置")
    @GetMapping("{id}/permission")
    public R<SysRolePermissionVo> getRolePermission(@PathVariable Long id) {
        return R.data(() -> roleService.getRolePermission(id));
    }

    @Operation(summary = "更新角色的数据权限配置")
    @PutMapping("{id}/permission")
    public R updateRolePermission(@PathVariable Long id, @RequestBody SysRolePermissionVo vo) {
        vo.setRoleId(id);
        return R.run(() -> roleService.updateRolePermission(vo));
    }

    @Operation(summary = "移除指定角色上的指定用户")
    @DeleteMapping("{id}/users")
    public R<?> removeUsersInRole(@PathVariable Long id, @RequestBody IdListPo idListPo) {
        return R.run(() -> roleService.removeUsersInRole(idListPo.getIds(), id));
    }

    @Operation(summary = "在指定角色上的添加用户")
    @PostMapping("{id}/users")
    public R<?> addUsersToRole(@PathVariable Long id, @RequestBody IdListPo idListPo) {
        return R.run(() -> roleService.addUsersInRole(idListPo.getIds(), id));
    }

    @Operation(summary = "获取新版数据权限分组列表")
    @GetMapping("data2/groups")
    public R<?> getData2Group() {
        return R.data(data2Service.getGroups());
    }

    @Operation(summary = "获取新版数据权限模块功能列表")
    @GetMapping("data2/funcs")
    public R<?> getData2ByGroupId() {
        return R.data(data2Service.getFuncs());
    }

    @Operation(summary = "获取新版数据权限字段权限信息列表")
    @GetMapping("data2/fields")
    public R<?> getData2Fields() {
        return R.data(data2Service.getFields());
    }

    @Operation(summary = "获取指定角色的字段权限配置")
    @GetMapping("data2/{roleId}/fields")
    public R<?> getFieldByRoleId(@PathVariable Long roleId) {
        return R.data(data2Service.getFieldsByRoleId(roleId));
    }

    @Operation(summary = "更新指定角色的字段权限配置")
    @PutMapping("data2/{roleId}/fields")
    public R<?> putFieldByRoleId(@PathVariable Long roleId, @RequestBody List<RoleFieldItem> items) {
        return R.run(() -> data2Service.updateFieldsByRoleId(roleId, items));
    }

    @Operation(summary = "获取指定角色和分组下的整合权限数据")
    @GetMapping("data2/{roleId}/group/{groupId}")
    public R<?> getData2Map(@PathVariable(name = "roleId") Long roleId, @PathVariable(name = "groupId") Long groupId) {
        return R.data(data2Service.getDataMap(roleId, groupId));
    }

    @Operation(summary = "更新指定角色和分组下的整合权限数据")
    @PutMapping("data2/{roleId}/group/{groupId}")
    public R<?> getData2Map(@PathVariable(name = "roleId") Long roleId, @PathVariable(name = "groupId") Long groupId, @RequestBody SysRoleData2Map data) {
        return R.run(() -> data2Service.setDataMap(roleId, groupId, data));
    }
}
